﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Data
{
    public class EmployeeDao : IEmployeeDao
    {
        public Employee GetEmployeeByUserNamePassword(string userName, string password)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.Employees.FirstOrDefault(e => e.UserName == userName && e.Password == password);
            }
        }

        public List<Employee> GetAllEmployees()
        {
            List<Employee> employees;

            using (var context = new MonkeyWarrantyEntities())
            {
                employees = context.Employees.Where(x => x.UserName != "admin").OrderBy(x => x.Name).ToList();
            }

            return employees;
        }

        public Employee AddEmployee(Employee employee)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                context.Employees.AddObject(employee);
                context.SaveChanges();
            }

            return employee;
        }

        public void UpdateEmployee(Employee employee)
        {
            // throw new System.NotImplementedException();
            if (employee == null || employee.Id == 0) return;

            if (string.IsNullOrEmpty(employee.UserName) || string.IsNullOrEmpty(employee.Password))
            {
                throw new ApplicationException("UserNamePasswordRequired");
            }

            using (var context = new MonkeyWarrantyEntities())
            {
                var countExisted = context.Employees.Count(x => x.Id != employee.Id && x.UserName == employee.UserName);
                if (countExisted > 0)
                    throw new ApplicationException("UserNameIsExisted");

                context.Employees.Attach(employee);
                context.ObjectStateManager.ChangeObjectState(employee, EntityState.Modified);
                context.SaveChanges();
            }
        }

        public void DeleteEmployee(int employeeId)
        {
            if (employeeId <= 0) return;
            using (var context = new MonkeyWarrantyEntities())
            {
                var employee = context.Employees.First(g => g.Id == employeeId);

                context.DeleteObject(employee);

                context.SaveChanges();
            }
        }

        public int GetMaxId()
        {
            var maxId = 0;

            using (var context = new MonkeyWarrantyEntities())
            {
                var employee = context.Employees.OrderByDescending(x => x.Id).FirstOrDefault();
                if (employee != null) maxId = employee.Id;
            }

            return maxId;
        }

        public int GetCurrentIdentity()
        {
            long? identity;
            using (var context = new MonkeyWarrantyEntities())
            {
                identity = context.GetIdentity("Employees").SingleOrDefault();
            }

            return identity.HasValue ? (int)identity.Value : 0;
        }

        public Employee GetById(int id)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.Employees.FirstOrDefault(x => x.Id == id);
            }
        }

        public Employee GetByUserName(string userName)
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                return db.Employees.FirstOrDefault(x => x.UserName == userName);
            }
        }
    }
}
